package com.dudy.authdemo.model;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Table(name = "person")
public class Person implements UserDetails {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String username;

    private String password;

    /**
     * 加密的盐
     */
    private String salt;

    private String phone;
    private String email;

    @Column(name = "gmt_create_time")
    private Date gmtCreateTime;

    private String creator;

    @Column(name = "gmt_update_time")
    private Date gmtUpdateTime;

    private String updater;
    /**
     * 删除标记，0：未删除，1：已删除
     */
    @Column(name = "delete_flag")
    private Byte deleteFlag;

    /**
     * 用户状态，0：正常，1：冻结
     */
    @Column(name = "status")
    private Byte status;

    private String roles;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        String[] split = roles.split(",");
        if (split.length > 0) {
            List<SimpleGrantedAuthority> authorities = new ArrayList<>(split.length);
            for (String roleCode : split) {
                //添加用户角色
                authorities.add(new SimpleGrantedAuthority("ROLE_" + roleCode));
            }
            return authorities;
        }
        return null;
    }


    /**
     * 账号是否过期
     *
     * @return
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 是否冻结
     *
     * @return
     */
    @Override
    public boolean isAccountNonLocked() {
        return (byte) 1 != status;
    }

    /**
     * 密码是否过期
     *
     * @return
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    /**
     * @return id
     */
    public Long getId() {
        return id;
    }

    /**
     * @param id
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return username
     */
//    @Override
    public String getUsername() {
        return username;
    }

    /**
     * @param username
     */
    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

    /**
     * @return password
     */
//    @Override
    public String getPassword() {
        return password;
    }

    /**
     * @param password
     */
    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    /**
     * 获取加密的盐
     *
     * @return salt - 加密的盐
     */
    public String getSalt() {
        return salt;
    }

    /**
     * 设置加密的盐
     *
     * @param salt 加密的盐
     */
    public void setSalt(String salt) {
        this.salt = salt == null ? null : salt.trim();
    }

    /**
     * @return gmt_create_time
     */
    public Date getGmtCreateTime() {
        return gmtCreateTime;
    }

    /**
     * @param gmtCreateTime
     */
    public void setGmtCreateTime(Date gmtCreateTime) {
        this.gmtCreateTime = gmtCreateTime;
    }

    /**
     * @return creator
     */
    public String getCreator() {
        return creator;
    }

    /**
     * @param creator
     */
    public void setCreator(String creator) {
        this.creator = creator == null ? null : creator.trim();
    }

    /**
     * @return gmt_update_time
     */
    public Date getGmtUpdateTime() {
        return gmtUpdateTime;
    }

    /**
     * @param gmtUpdateTime
     */
    public void setGmtUpdateTime(Date gmtUpdateTime) {
        this.gmtUpdateTime = gmtUpdateTime;
    }

    /**
     * @return updater
     */
    public String getUpdater() {
        return updater;
    }

    /**
     * @param updater
     */
    public void setUpdater(String updater) {
        this.updater = updater == null ? null : updater.trim();
    }

    /**
     * 获取删除标记，0：未删除，1：已删除
     *
     * @return delete_flag - 删除标记，0：未删除，1：已删除
     */
    public Byte getDeleteFlag() {
        return deleteFlag;
    }

    /**
     * 设置删除标记，0：未删除，1：已删除
     *
     * @param deleteFlag 删除标记，0：未删除，1：已删除
     */
    public void setDeleteFlag(Byte deleteFlag) {
        this.deleteFlag = deleteFlag;
    }

    public Byte getStatus() {
        return status;
    }

    public Person setStatus(Byte status) {
        this.status = status;
        return this;
    }

    public String getPhone() {
        return phone;
    }

    public Person setPhone(String phone) {
        this.phone = phone;
        return this;
    }

    public String getEmail() {
        return email;
    }

    public Person setEmail(String email) {
        this.email = email;
        return this;
    }

    public String getRoles() {
        return roles;
    }

    public Person setRoles(String roles) {
        this.roles = roles;
        return this;
    }
}